//
//  ZHNewFeatureController.swift
//  ZHWeibo
//
//  Created by user on 15/12/8.
//  Copyright © 2015年 heima. All rights reserved.
//

/**
  * 创建4个ImageView添加到scrollView上
  * 设置分页,contensize,滚动方向,bounces,
  * 添加pageControl,设置pageControl的基本属性,pagecontrol本身有frame
  * 在代理方法中,监听偏移量,注意偏移量的四舍五入,从而计算pageControl
  * 添加两个按钮,注意按钮的约束
*/

import UIKit

let FromNewToWelController = "FromNewToWelController"

class ZHNewFeatureController: UIViewController, UIScrollViewDelegate{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    private func setUpUI() {
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
        /**
        *  下面这句为什么会成功
        */
        pageControl.frame = CGRect(x: scrollView.frame.width * 0.5  , y: scrollView.frame.height - 100, width: 0, height: 0)
        view.addSubview(pageControl)
        
        let count = 4
        for i in 0...count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            imageView.frame = CGRect(x: CGFloat(i) * scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(imageView)
            if i == count-1 {
                setLastPage(imageView)
            }
        }
        scrollView.contentSize = CGSize(width: CGFloat(count) * scrollView.frame.width, height: 0)
        pageControl.numberOfPages = count

    }
    
    private func setLastPage(lastPage: UIView){
        /**
          * UIimageView可以添加子控件,但是必须开启用户交互功能
        */
        lastPage.userInteractionEnabled = true
        enterBtn.center = CGPoint(x: lastPage.frame.width * 0.5, y: lastPage.frame.height - 150)
        shareBtn.center = CGPoint(x: enterBtn.center.x, y: CGRectGetMinY(enterBtn.frame) - 30)
        lastPage.addSubview(enterBtn)
        lastPage.addSubview(shareBtn)
        
    }
    
    //控件懒加载
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    private lazy var pageControl: UIPageControl = {
        /**
        *  默认情况下,子控件如果超出父控件的范围,是可以看得到.-------这句话是什么意思
        */
        // 指定 clipsToBounds 代表把超出部分干掉
        // pageControl.clipsToBounds = true
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        return pageControl
    }()
    private lazy var shareBtn: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "sharedClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setTitle(" 分享到微博", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        /**
        *  这种方式的弊端:button的center的位置改变了
        */
        //button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
        button.sizeToFit()
        return button
    }()
    private lazy var enterBtn: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setTitle("进入微博", forState: UIControlState.Normal)
        
        button.sizeToFit()
        return button
    }()
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.frame.width + 0.5
        pageControl.currentPage = Int(page)

    }
    
    @objc private func sharedClick(btn: UIButton) {
        btn.selected = !btn.selected
        print("微博分享");
    }
    @objc private func enterClick() {
        //通知切换到wel控制器
        NSNotificationCenter.defaultCenter().postNotificationName("FromNewToWelController", object: nil)
    }

}










